let systemInfo = uni.getSystemInfoSync();
export default {
	data() {
		return {
			statusBarHeight: systemInfo.statusBarHeight,
			city: '全国',
			orderType: '',
			shopList: [],
			order: '',
			sortData: [{
				text: '综合',
				value: ''
			}, {
				text: '销量',
				value: 'shop_sales'
			}, {
				text: '附近',
				value: 'distance'
			}],
			keyword: '',
			lat: 0,
			lng: 0,
			iphoneX: false,
			hasFollow: false,
			category_id: '',
			classifies: [],
			website_id: '',
			mescroll: null,
			siteId: 0,
		}
	},
	computed: {
		swiperType() {
			let style = {};
			if (this.classifies.length < 6) {
				style.height = '200rpx';
			} else {
				style.height = '360rpx';
			}
			return style;
		},
		scroollTop() {
			let top = 610;
			if (this.classifies.length < 6) {
				top = 440;
			}
			// #ifdef MP
			top = top - (-30);
			// #endif
			if (this.$util.uniappIsIPhoneX()) {
				// #ifdef MP
				top = top - (-50);
				// #endif
			}
			// #ifdef APP || APP-PLUS
			top = 660;
			// #endif
			// top = 800;
			return top;
		}
	},
	onLoad(options) {
		this.keyword = options.keyword || '';
		this.siteId = parseInt(options.site_id) || 0;
		this.iphoneX = this.$util.uniappIsIPhoneX()
		this.getShopCate();
	},
	async onShow() {
		// 刷新多语言
		this.$langConfig.refresh();
		await this.getLbs();
		if (this.mescroll) {
			this.mescroll.resetUpScroll();
		}
		this.updateLocation();
	},
	methods: {
		async getLbs() {
			if (!uni.getStorageSync('location')) {
				return new Promise((resolve, reject) => {
					let _this = this;
					uni.getLocation({
						type: 'gcj02',
						success: res => {
							uni.setStorageSync('location', {
								latitude: res.latitude,
								longitude: res.longitude
							});
							_this.lat = uni.getStorageSync('location').latitude;
							_this.lng = uni.getStorageSync('location').longitude;
							_this.lbsGetCity();
							resolve(res);
						},
						fail: function(res) {
							console.log(res)
							_this.$util.showToast({
								title: '获取定位失败'
							});
							reject(res);
						}
					});
				});
			} else {
				this.lat = uni.getStorageSync('location').latitude;
				this.lng = uni.getStorageSync('location').longitude;
				if (uni.getStorageSync('city')) {
					this.city = uni.getStorageSync('city').title;
					this.website_id = uni.getStorageSync('website_id');
				} else {
					this.lbsGetCity();
				}
			}
		},
		lbsGetCity() {
			return new Promise((resolve, reject) => {
				//获取定位区域
				this.$api.sendRequest({
					url: '/api/address/lbsGetCity',
					data: {
						user_latitude: this.lat,
						user_longitude: this.lng
					},
					success: res => {
						if (res.code == 0) {
							let data = res.data;
							uni.setStorageSync('city', data['location']);
							uni.setStorageSync('website_id', data['website_id']);
							this.city = uni.getStorageSync('city').title;
							this.website_id = uni.getStorageSync('website_id');
							this.$refs.mescroll.refresh();
						}
						resolve(res);
					}
				})
			});
		},
		updateLocation() { //更新用户地址
			this.$api.sendRequest({
				url: '/api/member/setUserWebsiteInfo',
				data: {},
				success: res => {}
			})
		},
		turnClassify(value) {
			if (this.category_id == value) {
				this.category_id = '';
			} else {
				this.category_id = value;
			}
			this.mescroll.resetUpScroll();
		},
		getShopCate() {
			this.$api.sendRequest({
				url: '/api/shopcategory/getList',
				data: {},
				success: res => {
					if (res.code == 0 && res.data) {
						this.classifies = [...this.classifies, ...res.data];
					}
				}
			});
		},
		follow() {
			if (!uni.getStorageSync('token')) {
				this.$util.redirectTo('/otherpages/login/login/login', {});
				return;
			}
			if (this.hasFollow) {
				uni.showModal({
					title: '提示',
					content: '真的要取消关注嘛',
					success: function(res) {
						if (res.confirm) {
							this.$api.sendRequest({
								url: '/api/shopmember/delete',
								data: {
									site_id: this.shopList.site_id
								},
								success: res => {
									if (res.code == 0 && res.data) {
										this.hasFollow = !this.hasFollow;
										this.$util.showToast({
											title: '取消成功'
										});
									}
								}
							});
						}
					}
				});
			} else {
				this.$api.sendRequest({
					url: '/api/shopmember/add',
					data: {
						site_id: this.shopList.site_id
					},
					success: res => {
						if (res.code == 0 && res.data) {
							this.hasFollow = !this.hasFollow;
							this.$util.showToast({
								title: '关注成功'
							});
						}
					}
				});
			}
		},
		isFollow() {
			this.$api.sendRequest({
				url: '/api/shopmember/issubscribe',
				data: {
					site_id: this.shopList.site_id
				},
				success: res => {
					if (res.code == 0) {
						this.hasFollow = res.data;
					}
				}
			});
		},
		getShopList(mescroll) {
			this.mescroll = mescroll
			this.$api.sendRequest({
				url: '/api/shop/page',
				data: {
					page: mescroll.num,
					page_size: mescroll.size,
					keyword: this.keyword,
					order: this.order,
					sort: this.sort,
					lat: this.lat,
					lng: this.lng,
					category_id: this.category_id,
					website_id: this.website_id,
					city_id: uni.getStorageSync('city').id ? uni.getStorageSync('city').id : ''
				},
				success: res => {
					let newArr = []
					let msg = res.message;
					if (res.code == 0 && res.data) {
						newArr = res.data.list;
						for (var i = 0; i < newArr.length; i++) {
							newArr[i].composite_score = ((parseFloat(newArr[i].shop_desccredit) +
								parseFloat(newArr[i].shop_servicecredit) +
								parseFloat(newArr[i].shop_deliverycredit)) / 3).toFixed(1);
						}
					} else {
						this.$util.showToast({
							title: msg
						})
					}
					mescroll.endSuccess(newArr.length);
					//设置列表数据
					if (mescroll.num == 1) this.shopList = []; //如果是第一页需手动制空列表
					this.shopList = this.shopList.concat(newArr); //追加新数据
					if (this.$refs.loadingCover) this.$refs.loadingCover.hide();
				},
				fail: res => {
					mescroll.endErr();
					if (this.$refs.loadingCover) this.$refs.loadingCover.hide();
				}
			});
		},
		//筛选点击
		sortTabClick(tag) {
			if (tag == 'shop_sales') {
				this.order = 'shop_sales';
				this.sort = 'desc';
			} else if (tag == 'shop_desccredit') {
				this.order = 'shop_desccredit';
				this.sort = 'desc';
			} else if (tag == 'distance') {
				this.order = 'distance';
				this.sort = 'asc';
			} else {
				this.order = '';
				this.sort = '';
			}

			this.orderType = tag;
			this.mescroll.resetUpScroll();
		},
		search() {
			this.mescroll.resetUpScroll();
		},
		imgError(index) {
			this.shopList[index].logo = this.$util.getDefaultImage().default_shop_img;
			this.$forceUpdate();
		},
		phone(e) {
			uni.makePhoneCall({
				phoneNumber: e //仅为示例
			});
		},
		jump(e) {
			this.$util.redirectTo(e)
		},
		GetDistance(lat1, lng1, lat2, lng2) {
			var radLat1 = lat1 * Math.PI / 180.0;
			var radLat2 = lat2 * Math.PI / 180.0;
			var a = radLat1 - radLat2;
			var b = lng1 * Math.PI / 180.0 - lng2 * Math.PI / 180.0;
			var s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) +
				Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(Math.sin(b / 2), 2)));
			s = s * 6378.137; // EARTH_RADIUS;
			s = Math.round(s * 10000) / 10000;
			return s;
		},
		toDetail(item) {
			this.$util.redirectTo('/otherpages/shop/index/index', {
				site_id: item.site_id
			});
		},
		goodsImg(imgStr) {
			let imgs = imgStr.split(',');
			return imgs[0] ? this.$util.img(imgs[0], {
				size: 'mid'
			}) : this.$util.getDefaultImage().default_goods_img;
		}
	}

}